package com.eystar.ext.aop;

import com.eystar.ext.kit.RequestKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.log.Log;

public class ExceptionInterceptor implements Interceptor {
    protected final Log logger = Log.getLog(ExceptionInterceptor.class);
    private String render;
    
    public ExceptionInterceptor(String render){
    	this.render = render;
    }
    
    @Override
    public void intercept(Invocation ai) {
        try {
            ai.invoke();
        } catch (Exception e) {
        	logger.error(e.getMessage(),e);
        	Controller controller = ai.getController();
        	if (RequestKit.isAjaxRequest(controller.getRequest())) {
				controller.renderJson(Ret.fail("msg","程序内部错误！"));
			}else {
				controller.setAttr("msg", e.getMessage());
	        	controller.render(this.render);
			}
        }
    }

}
